home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Intro to STL / Speaking notes / Speaking notes.text next >
Encoding:
Text File  |  1997-06-28  |  26.0 KB  |  564 lines  |  [TEXT/MSWD]

  1. Introduction to STL for Macintosh Programmers
  2. Good afternoon, I would like to start by introducing myself. I am Jon Kalb and I work for Liberty Software a Macintosh-only Software Consulting firm in the bay area.
  3. Although I have written shipping code that makes heavy used of the Standard Template Library, I don't hold myself out as an expert. My goal for this session is not explore STL in detail or to get into advanced or even intermediate topics. My goal is to provide an introduction to experienced C++ programmers that have little or no experience with STL.
  4. If you are an experienced STL user, that's great because, later in this talk I will give you a chance to share some of your hard-won knowledge.
  5. STL, or the Standard Template Library, is now a part of the draft ANSI C++ Standard. Users of STL can begin to expect it to be universally available. For those of us that are not yet familiar with STL, the future is clear-professional C++ programmers will be expected to understand and maintain objects that depend on this new library.
  6. STL code will probably look a little strange at first and learning how to use the library will require some effort. Fortunately, STL's power, ease of use, and consistency make it worthwhile.
  7. My focus here is going to be on giving you the information you need to begin to use STL in your code. This discussion will be about practice, not theory, which is, in a way, a shame, because the thinking behind how the library is structured and used is very interesting and fun to explore. You will see that very little knowledge of templates is needed for getting started. Only when you are ready to extend the library is writing templates required.
  8. Before we plunge into the code-level nitty-gritty, I do want to give us an overview of what it is that STL is trying to accomplish. I feel that this perspective is valuable, because I know how lost I was without it.
  9. I was first drawn to STL as a standard container library. But as I read about STL, I was impatient that STL writers tend to start off by talking about generic algorithms, function objects, and/or iterators. If STL is about building a better list clas, than why start off with these side topics?
  10. It turns out that STL is not about containers. STL is really about generic algorithms. Containers are just a necessary step to reaching generic algorithm nirvana.
  11. As an example of a generic algorithm, consider qsort(). qsort() is an ANSI C implementation of a generic algorithm. Regardless of the contents of your array, your data is sorted.
  12. qsort() from <stdlib.h> An ANSI C Generic Algorithm
  13.  
  14.  
  15.     void qsort(    void *base,
  16.                 size_t nmemb,
  17.                 size_t size,
  18.                 int(*compare)(const void *, const void *));
  19.  
  20. where:    base    is a pointer to an array
  21.         nmemb    is the number of elements in the array
  22.         size    is the size of each array element
  23.         compare    is a pointer to a function defined as:
  24.  
  25.                     int compare(const void *e1, const void *e2);
  26.  
  27.                 where:    e1 and e2 are elements pointers
  28.                 returns            if
  29.                     < 0        (*e1) <  (*e2)
  30.                       0        (*e1) == (*e2)
  31.                     > 0        (*e1) >  (*e2)
  32.  
  33.             When subtraction is a valid operations for elements
  34.             of type T, the compare funtion can be implemented as:
  35.  
  36.             {
  37.                 return (int)((*(T *)e1) - (*(T *)e2));
  38.             }
  39.  
  40. But consider the limitation of this kind of generic algorithm.
  41. First, every use of qsort() requires that we code a "compare" function, even if this function is trivial. Of course, if the compare is trivial, then dereferencing a function pointer is pretty inefficient. Whether or not we care depends on the relative time spent in compares as opposed to swaps. For most uses of qsort() this may not be significant, but it points up a weakness of this approach to generic algorithms.
  42. Next, we have no control over construction, destruction, or assignment of the array items being sorted. All swapping is done by raw memory moves.
  43. Also, by using void pointers, we have completely sacrificed type safety.
  44. Finally, we are restricted to working with arrays and these must exist in the standard memory model. God, and the ANSI Standard, forbid that you use an unconventionally memory model or that your data be in a list, a tree, a file, or any container other than a simple array.
  45. These limitations can all be overcome, and have been in STL, by the use of templates.
  46. SLIDE OFF
  47. Before looking at the STL equivalent of qsort() I want to present a quick and dirty definition.
  48. One of the basic building blocks of STL is the iterator. For now, I want to define an iterator as anything that can take the deference operator or, to put it another way, if it can take pointer syntax, it's an iterator.
  49. quick and dirty iterator definition
  50.  
  51.  
  52.         If this makes sense:
  53.  
  54.             object == *thing;
  55.  
  56.         Then "thing" is an iterator.
  57.  
  58.  
  59.             char *a = "";
  60.                 '\0' == *a;
  61.  
  62.         All pointers are iterators.
  63.  
  64. For now, when you hear iterator, think pointer.
  65. Now let's look at STL's version of the quick sort algorithm:
  66. STL sort from <algo.h> or <algorithm> An STL Generic Algorithm
  67.  
  68.  
  69.  
  70.     template <class RandomAccessIterator>
  71.         void sort(    RandomAccessIterator first,
  72.                     RandomAccessIterator last)    {...}
  73.  
  74.  
  75.     template <class RandomAccessIterator, class Compare>
  76.         void sort(    RandomAccessIterator first,
  77.                     RandomAccessIterator last,
  78.                      Compare comp) {...}
  79.  
  80.  
  81. where our quick and dirty translation is:
  82.  
  83.         void sort(pointer first, pointer last);
  84.             (Uses "<" to compare elements.)
  85.  
  86.         void sort(pointer first, pointer last, Compare funct *comp);
  87.  
  88.  
  89. Instead of passing an array's base address and the number and size of elements in the array we pass a pair of iterators (think pointers). Due to template magic, these iterators are type safe. We need not pass in the element size because, like type safe pointers, we can safely increment and decrement as needed.
  90. Passing two iterators is the standard way of passing a collection of items in STL. The first iterator refers to the first item in the collection, but the second iterator, despite its name, does not refer to the last. The convention is that the second iterator points one past the last item in the collection.
  91. Using this convention, the two iterators exactly enclose all and only the elements to be considered.
  92. iterating over an STL collection
  93.  
  94.  
  95.     A set of data is represented in STL by a pair of
  96.     iterators. The first iterator references the first
  97.     element and the last iterator references one past
  98.     the last element. This is represented in half-open
  99.     interval notation as:
  100.  
  101.             [first, last)
  102.  
  103.  
  104.     Iterating:
  105.  
  106.     Instead of the typical C iteration:
  107.  
  108.     for (i = 0; i < count; ++i)
  109.     {
  110.         arrayBase[i]; /* element */
  111.     }
  112.  
  113.     We use:
  114.  
  115.     for (i = first; i != last; ++i)
  116.     {
  117.         (*i);    // dereferenced object
  118.     }
  119.  
  120.     Never dereference the "last" iterator. It can
  121.     only be used for comparing with the "first"
  122.     iterator.
  123.     
  124.         (*last) is undefined.
  125.  
  126. This may take some getting used to at first, but, on reflection, it makes a great deal of sense. Because the iterators may be pointers into a list, we can't rely on the standard "less than" comparison that we are used to. As we iterate over our collection, we just need to increment our "first" iterator and compare it to our "last" iterator. When the iterators are equal (which, by definition, means that they refer to the same item), then we are done.
  127. Note that we never dereference the "last" iterator. It may be nil or any other value. It can only be used to compare with the "first" iterator. You should always assume that dereferencing it will bus error.
  128. STL sort from <algo.h> or <algorithm> An STL Generic Algorithm
  129.  
  130.  
  131.  
  132.     template <class RandomAccessIterator>
  133.         void sort(    RandomAccessIterator first,
  134.                     RandomAccessIterator last)    {...}
  135.  
  136.  
  137.     template <class RandomAccessIterator, class Compare>
  138.         void sort(    RandomAccessIterator first,
  139.                     RandomAccessIterator last,
  140.                      Compare comp) {...}
  141.  
  142.  
  143. where our quick and dirty translation is:
  144.  
  145.         void sort(pointer first, pointer last);
  146.             (Uses "<" to compare elements.)
  147.  
  148.         void sort(pointer first, pointer last, Compare funct *comp);
  149.  
  150.  
  151. Returning to the STL sort declarations, we see that the first declaration does not take a comparison functio. If no comparison function is passed, the STL sort implementation will simply use the "less than" operator for comparison (of the items, not the iterators). At first glance, this may seem to be of little use, but since template magic gives us type safety, the objects to be sorted can overload the "less than" operator. Without our having to code a comparison function, the objects are sorted into their "natural" order.
  152. SLIDE OFF
  153. In our first sample program I demonstrate the efficiency that we can achieve with templates. Often new coding technologies offer us a trade-off with power of expression or ease of use on one side and performance on the other side. This program compares quick sorting an array with qsort() and the STL sort() generic algorithm.
  154. #include <iostream>
  155.  
  156. int    ANSICqsortCompare(const void *el1, const void *el2);
  157. int    ANSICqsortCompare(const void *el1, const void *el2)
  158. {
  159.     return (int)(*(long *)el1 - *(long *)el2);
  160. }
  161.  
  162. void main(void)
  163. {
  164.     cout << "STL sort sample!\n\n";            // setting up arrays
  165.     const    size_t    aSize =  10240;
  166.     long    *qArray = new long[aSize];
  167.     long    *stlArray = new long[aSize];
  168.     assert(qArray && stlArray);
  169.  
  170.     srand(time(NULL));                        // randomize
  171.     for (int i = 0; i < aSize; ++i)
  172.     {
  173.         qArray[i] = stlArray[i] = rand();
  174.     }
  175.  
  176.     long    qsortTickStart = clock();        // ANSI sort
  177.     qsort(qArray, aSize, sizeof(long), ANSICqsortCompare);
  178.     long    qTix = clock() - qsortTickStart;
  179.  
  180.     long    stlTickStart = clock();            // STL sort
  181.     sort(stlArray, &stlArray[aSize]);
  182.     long    stlTix = clock() - stlTickStart;
  183.  
  184.     for (int i = 0; i < aSize; ++i)            // verify
  185.     {
  186.         assert(qArray[i] == stlArray[i]);
  187.     }
  188.     cout << "qsort() " << qTix << "\nSTL sort " << stlTix << "\n";
  189.     delete [] qArray; delete [] stlArray;
  190. }
  191.  
  192. I tested this on both 68K and PowerPC machines with both the HP STL and the Modina STL implementations. The STL sort was from three to twelve times faster.
  193. Of course, I stacked the deck with this sample program. Because the objects being sorted were of a built-in type (they are longs) and because we used the form of sort() that does not use a "compare" function object we don't have any function call overhead when doing compares. But that is my point. Template magic allows us to implement a generic algorithm that, in some cases, doesn't require functional call overhead, when the traditional approach would always require it.
  194. My point is not that generic algorithms are more efficient than specifically coded algorithms my point is that, using templates, we can be more efficient than using the traditional approaches to generic algorithms.
  195. SLIDE OFF
  196. Now that we have tasted generic algorithms, we have a better idea of why implementing them leads us to iterators and containers. The whole point of generic algorithms is that the work that needs to be done does not depend on the kind of data involved.
  197. If a class of objects supports addition, then we can accumulate. If it supports addition and division by a scalar then we can average. If it supports the "less than" operator, then we can sort. It doesn't matter if the class represents ints, floats, strings, test scores, portfolios, medical records, or box scores. It also doesn't matter if the items are stored in an array, a list, a file, or on a web server.
  198. If our generic algorithms only worked on items that are passed to them, like min and max, then we would have no need of iterators or containers.
  199. Most generic algorithms are designed for an arbitrary number of items. This is why we have iterators. With this single piece of information we can get the value of an item (by dereferencing) or move to the next item in the collection (by incrementing or decrementing).
  200. SLIDE OFF
  201. If the only containers that we ever used were arrays, then we could just use pointers and forget about this iterator nonsense. The one drawback to pointers is that they don't hide the container from the generic algorithm. Because qsort() uses pointers, it can only operate on arrays.
  202. Imagine a linked list. A pointer to an element in the list has part of what we need in an iterator--if we dereference it we have our element. But if we increment this pointer we don't get the next item in the list, we are pointing at some arbitrary memory location.
  203. Now imagine that the we have defined a class of objects that point at items in lists. We can override the increment operator to have the object pointer walk the list. Voila,! We have a list iterator.
  204. This also explains why iterators and containers are so closely linked. The implementation of the iterator is dependent on the nature of the container class.
  205. We are almost ready to get down and dirty with some code, but before we start looking at containers we need to be aware of some restrictions placed on the objects that will be contained.
  206. Theoretical class requirements
  207.  
  208. Classes whose instances will be stored in STL containers must have:
  209.  
  210.     • a copy constructor
  211.     • operator=
  212.  
  213. If the instances will be sorted or compared then the class must have:
  214.  
  215.     • operator==
  216.     • operator<
  217.  
  218. Practical class requirements
  219.  
  220. In practice, it may be necessary to please the compiler gods by
  221. defining some functions that are never called. The practical list of
  222. requirements is:
  223.  
  224.     • a default constructor
  225.     • a copy constructor
  226.     • operator=
  227.     • operator==
  228.     • operator<
  229.  
  230.  
  231. OK, let's look at containers and their iterators. We already know that we can use arrays and pointers with generic algorithms, now lets look as the other containers that STL defines.
  232. The first containers are called vectors and deques. Vector is not a terrible intuitive name (at least it wasn't for me). The idea is that it grows in only one direction. A vector is like an array except that it can grow, but only at the end. A deque is like an array except that it can grow at both the beginning and the end.
  233. #include <iostream>
  234. #include <deque>
  235.  
  236. void main(void)
  237. {
  238. //    vector<char *>    v;        // This should be OK. It isn't yet.
  239.     vector<char *, allocator<char *> >    v;    // Required in CW12
  240. //    vector<char *, allocator<char *>>    v;    // ">>" is extraction
  241.  
  242.     assert(v.empty());
  243.     v.push_back("sleep");
  244.     v.insert(v.end(), "was");
  245.     assert(v.size() == 2);
  246.     v.push_back("for");
  247.     v.push_back("the");
  248.     v.push_back("weak");
  249.     v.push_back("or");
  250.     v.pop_back();
  251.     v.push_back("and");
  252.     v.push_back("sickly");
  253.     v[1] = "is";        // Used to replace existing: "is" for "was"
  254. //    v[7] = "DOS users";    // Cannot use this notation to add elements.
  255.  
  256.     vector<char *, allocator<char *> >::iterator vi;
  257.     for (vi = v.begin(); vi != v.end(); ++vi)
  258.     {
  259.         cout << (*vi) << " ";
  260.     }
  261.     cout << endl;
  262.  
  263.     typedef deque<char *, allocator<char *> > MyDeque;
  264.     MyDeque    d(v.begin(), v.end());
  265.  
  266.     d.erase(d.end() - 3, d.end() - 1);
  267.     d.push_front("MacHack:");
  268. //    ostream_iterator<char *>out(cout, " ");    // This should be OK.
  269.     ostream_iterator<char *, char, char_traits<char> >    out(cout, " ");
  270.     copy(d.begin(), d.end(), out); cout << endl;
  271. }
  272. // sleep is for the weak and sickly 
  273. // MacHack: sleep is for the sickly 
  274.  
  275. In this sample program we play with a vector and a deque.
  276. As the saying goes, "In theory-theory is as good as practice… In practice, it isn't." The STL spec says that we should be able to declare our vector, v as "vector left angle char splat right angle."
  277. As of CW12 or CWPro 1, Metrowerks is not yet quite up to the STL spec, so, for now, we have to add an allocator declaration to each STL container declaration. The second template parameter is supposed to be a default parameter. Since this isn't yet supported as a default parameter, we must specify it explicitly.
  278. Allocators do not fall into scope of this introduction so we won't be saying anymore about them. Just trust me on this, for each container class add ", space allocator left-angle your type right-angle space." Don't forget the last space or your two right angle brackets will merge and become the extraction operator. You don't want to know what your compiler thinks of that.
  279. This code demonstrates several functions of vectors and deques:
  280. empty()
  281. which works the way you would expect for all containers.
  282.  
  283. push_back()
  284. which is the workhorse for adding to a vector
  285.  
  286. insert()
  287. which, although I am using it here to insert at the end of the vector, can be used to insert anywhere. Inserting in the middle of a vector or deque can be done, but it is not efficient. If you need to do this often, consider using a list.
  288.  
  289. pop_back()
  290. erases the last item in the vector or deque.
  291.  
  292. array notation
  293. both vectors and deques use array notation. This notation returns a reference so it can be used for both reading and writing, but it cannot be used to add elements to the container.
  294.  
  295. erase()
  296. which can be called to erase a collection by passing two iterators. I want to erase the elements that are the third from the end to the second to the end, but since the iterator should point passed the last item to be erased, I passed d.end() - 3 and d.end() - 1 for the range.
  297.  
  298.  
  299. Note how we declare an iterator for our container. We simply restate the container declaration followed by "scope resolution operator iterator." As container declarations tend to get a bit wordy, we often use a typedef to make life easier. I have done that for the deque declaration.
  300. Iterating over the container is just as we discussed earlier and it is identical for both the vector and the deque.
  301. The only other things to notice are the deque constructor, we construct by iterating across the vector, and the push_front() call. Deques can grow at both ends and the efficient way to insert at the beginning of the deque is push_front(). There is also a pop_front. These call is not supported for vectors.
  302. SLIDE OFF
  303. Vectors, deques, and arrays are called sequence containers because they store items in the sequence in which they are added. With one exception, other STL containers store items in sorted order and are called associative containers. The last sequence container is the list.
  304. #include <iostream>
  305. #include <list>
  306.  
  307. void main(void)
  308. {
  309.     typedef    list<char, allocator<char> > MyList;
  310.     char    start[] = "machack";
  311.     MyList    l(start, start + strlen(start));
  312.  
  313.     ostream_iterator<char, char, char_traits<char> >    out(cout);
  314.     cout << "  start: ";
  315.     copy(l.begin(), l.end(), out);
  316.  
  317.     cout << "\nremoved: ";
  318.     l.remove('c');
  319.     copy(l.begin(), l.end(), out);
  320.  
  321.     cout << "\n sorted: ";
  322.     l.sort();
  323.     copy(l.begin(), l.end(), out);
  324.  
  325.     cout << "\n unique: ";
  326.     l.unique();
  327.     copy(l.begin(), l.end(), out);
  328.  
  329.     cout << "\nreverse: ";
  330.     l.reverse();
  331.     copy(l.begin(), l.end(), out);
  332.     cout << "\n";
  333. }
  334. //   start: machack
  335. // removed: mahak
  336. //  sorted: aahkm
  337. //  unique: ahkm
  338. // reverse: mkha
  339.  
  340. In this example we see that using a list is similar to the other containers. The biggest differences are in the things that you already know about the differences between array-like classes and lists. Arrays have less overhead and lists are more flexible.
  341. Here we are showing off remove(), sort(), unique(), and reverse(), which are member functions of the list class. This class also has functions to splice() lists together or merge() sorted lists.
  342. But in going from array-like containers to lists we did have to give up some things. We can no longer use array notation or the at() function to address items by their position in the list.
  343. The rest of the STL containers are associative. These containers keep the items they contain in sorted order. As you would expect, at construction, either pass a function to use as a compare function or we use the default "less than" comparison.
  344. Because these containers are always sorted, we no longer use push_back() or push_front(). We can call insert() without passing a positions iterator.
  345. #include <iostream>
  346. #include <set>
  347.  
  348. void main(void)
  349. {
  350.     typedef    set<char, less<char>, allocator<char> > MySet;
  351.     typedef    multiset<char, less<char>, allocator<char> > MyMultiSet;
  352.     ostream_iterator<char, char, char_traits<char> >    out(cout);
  353.     char    start[] = "sleep is a poor substitute for caffeine";
  354.     MySet    s(start, start + strlen(start));
  355.     MyMultiSet    ms;
  356.     ms.insert(start, start + strlen(start));
  357.  
  358.     cout << "      start: ";
  359.     copy(s.begin(), s.end(), out);
  360.     cout << "\n      start: ";
  361.     copy(ms.begin(), ms.end(), out);
  362.  
  363.     MySet::iterator    si1, si2;
  364.     MyMultiSet::iterator    msi1, msi2;
  365.     si1 = s.lower_bound('b');
  366.     si2 = s.upper_bound('s');
  367.     s.erase(' ');
  368.     s.erase(si1, si2);
  369.     msi1 = ms.lower_bound('b');
  370.     msi2 = ms.upper_bound('s');
  371.     ms.erase(' ');
  372.     ms.erase(msi1, msi2);
  373.  
  374.     cout << "\nremoved b-s: ";
  375.     copy(s.begin(), s.end(), out);
  376.     cout << "\nremoved b-s: ";
  377.     copy(ms.begin(), ms.end(), out);
  378.     cout << "\n";
  379. }
  380. //       start:  abcefilnoprstu
  381. //       start:       aabceeeeefffiiilnooopprrsssstttuu
  382. // removed b-s: atu
  383. // removed b-s: aatttuu
  384.  
  385. In this example we use two of the associative containers, the set and the multiset. The set is a simple ordered collection that differs from the multiset in that it does not support duplicate items.
  386. Both set and multiset have lower_bound() and upper_bound() members. These members return an iterator that refers to the lowest or highest location that an item of the passed value could be placed without violating the ordering rules.
  387. The last two containers that we are going to examine are called the map and the multimap. As you have already guessed, the difference between them is that the multimap can contain duplicate entries.
  388. All of the containers that we have looked at so far have been designed to contain items, but the map is designed to contain pairs of times. Each pair consists of a key and a value. When a pair is added to a map the key is used to find where, in the sort order, this pair belongs.
  389. A map is like a set in which each item in the set has a link to another item that may be of a different type.
  390. A Visualization for sets and maps:
  391.  
  392.     set == ordered items w/o dups
  393.             1
  394.             4
  395.             7
  396.             9
  397.  
  398.     multiset ==ordered items w/dups
  399.             1
  400.             4
  401.             4
  402.             7
  403.             9
  404.  
  405.     map == order keys with values w/o dups
  406.             key    value
  407.             1 -- c
  408.             4 -- x
  409.             7 -- j
  410.             9 -- w
  411.  
  412.     multimap == order keys with values w/dups
  413.             key    value
  414.             1 -- c
  415.             4 -- x
  416.             4 -- d
  417.             7 -- j
  418.             9 -- w
  419.  
  420. #include <iostream>
  421. #include <map>
  422.  
  423. void main(void)
  424. {
  425.     int    integers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  426.     char *intNames[] = {"zero", "one", "two", "three",
  427.                         "four", "five", "six", "seven",
  428.                         "eight", "nine", "ten"};
  429.     typedef    map<int, char *, less<int>, allocator<char *> > MyMap;
  430.     typedef    vector<int, allocator<int> > MyVector;
  431.     MyMap    m;
  432.     for (int i = 0; i < 11; ++i)
  433.     {
  434.         m.insert(MyMap::value_type(integers[i], intNames[i]));
  435.     }
  436.     MyVector lhs(integers, integers + 11), rhs(integers, integers + 11);
  437.     random_shuffle(lhs.begin(), lhs.end());
  438.     random_shuffle(rhs.begin(), rhs.end());
  439.     for (int j = 0; j < 11; ++j)
  440.     {
  441.         int sum = lhs[j] + rhs[j];
  442.         int diff = lhs[j] - rhs[j];
  443.         if (sum < 11)
  444.         {
  445.             cout << m[lhs[j]] << " + " << m[rhs[j]] <<
  446.                     " = " << m[sum] << endl;
  447.         }
  448.         if (diff >= 0)
  449.         {
  450.             cout << m[lhs[j]] << " - " << m[rhs[j]] <<
  451.                     " = " << m[diff] << endl;
  452.         }
  453.     }
  454. }
  455. // two + zero = two
  456. // two - zero = two
  457. // eight - five = three
  458. // nine + one = ten
  459. // nine - one = eight
  460. // three + two = five
  461. // three - two = one
  462. // one + four = five
  463. // ten - ten = zero
  464. // zero + seven = seven
  465. // seven + three = ten
  466. // seven - three = four
  467.  
  468. In this example, we create a map that maps integers to their names. We create two vectors of integers and use the random_shuffle generic algorithm to shuffle their values. Next we walk the vectors and add and subtract their corresponding values. What makes it interesting is that when we print our results we use the map to replace all the integers with their names.
  469. We can use array notation so that the expression "map sub key" returns the map value.
  470. My final code example deals with exceptions. My best words to you are proceed with caution.
  471. #include <iostream>
  472. #include <deque>
  473.  
  474. #if comment
  475.  
  476.     The exception hierarchy:
  477.  
  478.         exception            root of all exceptions
  479.         |    |
  480.         |    bad_alloc        memory allocation exception
  481.         |
  482.         logic_error            precondition violation exception root
  483.         |    |    |
  484.         |    |    out_of_range    attempt to reference a container
  485.         |    |                    using an illegal index
  486.         |    |
  487.         |    length_error    attempt to create a string with an
  488.         |                    illegal length
  489.         |
  490.         invalid_argument    attempt ot create a container with an
  491.                             illegal size such as -1
  492. #endif    // comment
  493.  
  494. void main(void)
  495. {
  496.     const    int    limit = 10;
  497.     vector<int, allocator<int> > v;
  498.     for (int j = 0; j < limit; ++j)
  499.     {
  500.         v.push_back(j);
  501.     }
  502.     try
  503.     {
  504.         int i = v[limit];
  505.         cout << i << " no exception for i = v[limit];" << endl;
  506.         i = v.at(limit);
  507.         cout << i << " no exception i = v.at(limit);" << endl;
  508.         i = v.at(limit + 1);
  509.         cout << i << " no exception i = v.at(limit + 1);" << endl;
  510.     }
  511.     catch (out_of_range& error)
  512.     {
  513.         cout << "An out of range execption was caught" << endl;
  514.     }
  515. }
  516. // 1868783980 no exception for i = v[limit];
  517. // 1868783980 no exception i = v.at(limit);
  518. // An out of range execption was caught
  519.  
  520. As this example shows not every call will throw exceptions when you might expect. Here the vector's at() call will throw, but the array notation fails silently. Also the at() call does not throw until the caller is off by two rather than off by one.
  521. You have been warned!
  522. I want to leave you with a few tips:
  523.  
  524. Hints for STL coders.
  525.  
  526. Don't forget that the "last" iterator points "one past".
  527.  
  528. For your custom objects, always define:
  529.  
  530.     • a default constructor
  531.     • a copy constructor
  532.     • operator=
  533.     • operator<
  534.     • operator==
  535.  
  536. Objects are copied into contains. The original object still
  537. exist and may need to be deleted.
  538.  
  539. If it ain't broke break it. Better to learn how the complier complains
  540. when you do know what to fix.
  541.  
  542. Don't do relational compares on iterators. Only use == or !=.
  543.  
  544. Don't forget to match the const/non-constness or iterators and
  545. containers.
  546.  
  547. Watch remove(), it doesn't erase().
  548.  
  549. Watch nested templates--don't let them look like ">>".
  550.     vector<int, allocator<int>> v.
  551.  
  552. If you use the HP STL implementation beware of init_page_size in
  553. defalloc.h. It allocates 4K for every STL container that is created.
  554. This makes sense on UNIX, but it is probably not what you want.
  555.  
  556. We have looked at all of the STL container classes and briefly discussed generic algorithms. My hope is that you have enough information to begin to use STL in your code, but the list of items that we have not discussed is longer than the items that we have.
  557. STL is very complex and I have deliberately tried to steer us away from complicating issue. About the issues that we have discussed, I am now ready to take questions.
  558.  
  559. -----------------
  560. Possible question:
  561. Code Bloat
  562. •    Using Adaptors
  563. •    Using Pointer-To-Base-Class storage. (This is useful for avoiding code bloat, whether you are using templates or not.)
  564.